package persistencia;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import util.Conexion;
import bean.Articulo;

public class ArticuloDAO {

	public List<Articulo> buscarArticulo(Articulo articulo) throws SQLException {
		String query="select ifnull(trim(artcod),'') artcod, ifnull(trim(artdes),'') artdes, ifnull(artequ,'') artequ, ifnull(artmar,'') artmar, ifnull(artmed,'') artmed "
				+ "from speed400tc.tarti where arsitu in ('01','02') ";
		if(!articulo.getArtcod().isEmpty()){
			query+="and artcod like upper(?) || '%' ";
		}
		if(!articulo.getArtdes().isEmpty()){
			query+="and upper(artdes) like upper(?) || '%' ";
		}
		if(!articulo.getArtequ().isEmpty()){
			query+="and upper(artequ) like upper(?) || '%' ";
		}
		if(!articulo.getArtmar().isEmpty()){
			query+="and upper(artmar) like upper(?) || '%' ";
		}
		PreparedStatement pstm = Conexion.obtenerConexion().prepareStatement(query);
		int parametro = 0;
		if(!articulo.getArtcod().isEmpty()){
			pstm.setString(++parametro, articulo.getArtcod());
		}
		if(!articulo.getArtdes().isEmpty()){
			pstm.setString(++parametro, articulo.getArtdes());
		}
		if(!articulo.getArtequ().isEmpty()){
			pstm.setString(++parametro, articulo.getArtequ());
		}
		if(!articulo.getArtmar().isEmpty()){
			pstm.setString(++parametro, articulo.getArtmar());
		}
		ResultSet rs = pstm.executeQuery();
		List<Articulo> listado = new ArrayList<Articulo>();
		while(rs.next()){
			Articulo obj = new Articulo();
			obj.setArtcod(rs.getString("artcod"));
			obj.setArtdes(rs.getString("artdes"));
			obj.setArtequ(rs.getString("artequ"));
			obj.setArtmar(rs.getString("artmar"));
			obj.setArtmed(rs.getString("artmed"));
			listado.add(obj);
		}
		
		rs.close();
		pstm.close();
		return listado;
	}
}
